*******************************************************************************
******************************  Figure 5  *************************************
*********  Sources of undervoting for women: Votes per man and woman,**********
************  by sex of vote grantor and across conditions  *******************
*******************************************************************************

clear all
set more off

//cd "~/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_5"

*ssc install distinct

*******************************************************************************

use "figure_5_dataset.dta", clear

collapse (mean) *val *exp, by(female condition_maj_min)
reshape long @_val @_exp, i(female condition) j(variable) string
g mean = .
g sd = .
g N = .
g group_N = .
g pXexp = .

forval i = 1/`c(N)' {
//local i = 1
local f = female[`i']
local c = condition_maj_min[`i']
local v = variable[`i']
local exp = _exp[`i']

preserve
use "figure_5_dataset.dta", clear
sum `v'_val if female == `f' & condition_maj_min==`c', d
local xbar = `r(mean)'
local sd = `r(sd)'
local N = `r(N)'
ttest `v'_val = `exp' if female == `f' & condition_maj_min==`c'
local pXexp = `r(p)'
distinct studyXgroup if female == `f' & condition_maj_min==`c'
local group_N = `r(ndistinct)'
restore

replace mean = `xbar' if _n==`i'
replace sd = `sd' if _n==`i'
replace N = `N' if _n==`i'
replace pXexp = `pXexp' if _n==`i'
replace group_N = `group_N' if _n==`i'

}

g meanse = sd/sqrt(N)
g clustermeanse = sd/sqrt(group_N)
g tIID = (_val-_exp)/meanse
g tcluster = (_val-_exp)/ clustermeanse
g ci =  clustermeanse*1.96
g sex = cond(female==1, "Women", "Men")
g pval_cluster = (2 * ttail(group_N-1, abs((_val-_exp)/clustermeanse)))

export delimited using "figure_5_input.csv", delim(",") replace
